package cn.tedu.spotms.server.controller;

import cn.tedu.spotms.server.pojo.dto.AdminAddNewDTO;
import cn.tedu.spotms.server.pojo.dto.AdminLoginDTO;
import cn.tedu.spotms.server.pojo.dto.AdminMatchRolesDTO;
import cn.tedu.spotms.server.pojo.dto.AdminUpdateInfoDTO;
import cn.tedu.spotms.server.pojo.vo.AdminDetailVO;
import cn.tedu.spotms.server.pojo.vo.AdminListItemVO;
import cn.tedu.spotms.server.service.IAdminService;
import cn.tedu.spotms.server.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/admins",produces = "application/json;charset=utf-8")
@Api(tags = "管理员管理模块")
public class AdminController {

    @Autowired
    private IAdminService service;

    @PostMapping("/login")
    @ApiOperation("管理员登录")
    @ApiOperationSupport(order = 1)
    public JsonResult<String> login(AdminLoginDTO adminLoginDTO) {
        String jwt = service.login(adminLoginDTO);
        return JsonResult.ok(jwt);
    }

    @PostMapping("/add-new")
    @ApiOperation("添加管理员")
    @ApiOperationSupport(order = 10)
    @PreAuthorize("hasAuthority('/admin/update')")
    public JsonResult<Void> addNew(@Validated AdminAddNewDTO adminAddNewDTO) {
        service.addNew(adminAddNewDTO);
        return JsonResult.ok();
    }

    @PostMapping("/match-roles")
    @ApiOperation("给管理员配置角色信息")
    @ApiOperationSupport(order = 50)
    @PreAuthorize("hasAuthority('/admin/update')")
    public JsonResult<Void> matchRoles(AdminMatchRolesDTO adminMatchRolesDTO) {
        service.matchRolesByAdminId(adminMatchRolesDTO);
        return JsonResult.ok();
    }

    @GetMapping("/delete/{id}")
    @ApiOperation("根据id删除管理员")
    @ApiOperationSupport(order = 100)
    @PreAuthorize("hasAuthority('/admin/delete')")
    public JsonResult<Void> deleteById(@PathVariable Long id) {
        service.deleteById(id);
        return JsonResult.ok();
    }

    @GetMapping("/disable/{id}")
    @ApiOperation("根据id禁用管理员")
    @ApiOperationSupport(order = 200)
    @PreAuthorize("hasAuthority('/admin/update')")
    public JsonResult<Void> disableById(@PathVariable Long id) {
        service.disableById(id);
        return JsonResult.ok();
    }

    @GetMapping("/enable/{id}")
    @ApiOperation("根据id启用管理员")
    @ApiOperationSupport(order = 205)
    @PreAuthorize("hasAuthority('/admin/update')")
    public JsonResult<Void> enableById(@PathVariable Long id) {
        service.enableById(id);
        return JsonResult.ok();
    }

    @PostMapping("/update-info")
    @ApiOperation("修改管理员基本信息")
    @ApiOperationSupport(order = 250)
    @PreAuthorize("hasAuthority('/admin/self/update')")
    public JsonResult<Void> updateInfo(AdminUpdateInfoDTO admin) {
        service.updateInfo(admin);
        return JsonResult.ok();
    }

    @GetMapping("/select/{id}")
    @ApiOperation("查询管理员基本信息")
    @ApiOperationSupport(order = 500)
    @PreAuthorize("hasAuthority('/admin/self/read')")
    public JsonResult<AdminDetailVO> getDetailsById(@PathVariable Long id) {
        AdminDetailVO admin = service.getDetailsById(id);
        return JsonResult.ok(admin);
    }

    @GetMapping("/list")
    @ApiOperation("管理员列表")
    @ApiOperationSupport(order = 900)
    @PreAuthorize("hasAuthority('/admin/read')")
    public JsonResult<List<AdminListItemVO>> list() {
        List<AdminListItemVO> list = service.list();
        return JsonResult.ok(list);
    }
}
